package com.mi.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.log.annotation.Log;
import com.mi.system.dto.request.MenuRequestDTO;
import com.mi.system.entity.Menu;
import com.mi.system.service.MenuService;
import com.mi.system.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.mi.common.core.util.Result.ok;

/**
 * Menu controller
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2021/11/1 15:08
 */
@Api(value = "菜单API", tags = "Menu")
@ApiSort(997)
@RestController
@RequestMapping("/api/v1/menus")
@RequiredArgsConstructor
@Slf4j
public class MenuController {

    private final MenuService menuService;

    @ApiOperation(value = "查询菜单树", notes = "查询菜单树")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @Log(title = "查看菜单组织机构")
    @GetMapping("/trees")
    public Result<List<MenuVO>> listMenu() {
        log.debug("API CALL : get menu tree.");

        return ok(menuService.getMenuTree());
    }

    @ApiOperation(value = "获取个人菜单组织结构", notes = "获取个人菜单组织结构")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @Log(title = "获取个人菜单组织结构")
    @GetMapping("/trees/users")
    public Result<List<MenuVO>> getUserMenuTree() {
        log.debug("API CALL : get user menu tree.");

        return ok(menuService.getUserMenuTree());
    }

    @ApiOperation(value = "保存菜单", notes = "保存菜单")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 3)
    @Log(title = "新增菜单:#{#request.name}")
    @PostMapping
    public Result<Boolean> saveMenu(@Valid @RequestBody MenuRequestDTO request) {
        log.debug("API CALL : save menu.");

        return ok(menuService.saveMenu(request));
    }

    @ApiOperation(value = "更新菜单", notes = "更新菜单信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 4)
    @ApiImplicitParam(name = "id", value = "菜单的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "更新菜单:#{#request.name}")
    @PutMapping("/{id}")
    public Result<Boolean> updateMenu(@PathVariable Long id, @Valid @RequestBody MenuRequestDTO request) {
        log.debug("API CALL : update menu.");

        return ok(menuService.updateMenu(id, request));
    }

    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 5)
    @ApiImplicitParam(name = "id", value = "菜单的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "删除菜单:#{#request.name}")
    @DeleteMapping("/{id}")
    public Result<Menu> removeMenu(@PathVariable Long id) {
        log.debug("API CALL : remove menu.");

        return ok(menuService.removeMenu(id));
    }
}
